package com.example.kafka_and_sb.controller;

import com.example.kafka_and_sb.tool.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class KafkaController {
    @Resource
    RedisUtil redisUtil;

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @RequestMapping("/send")
    public String send(String name) {
        kafkaTemplate.send("topic1", name);
        return name;
    }

    @RequestMapping("/list")
    @ResponseBody
    public String afterKafka(){

        if(redisUtil.hasKey("kafka")) {
            String str = redisUtil.get("kafka").toString();

            if ("".equals(str))
                return "abcd";
            else
                return str;
        }

        return "ddd";
    }

    @RequestMapping("/redis")
    @ResponseBody
    public String redis(String str){

        kafkaTemplate.send("topic1", str);

        return "ddd";
    }

}
